package org.lc.stk.web.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
@Schema(description = "分页响应")
public class PageResponse<T> {

    @Schema(description = "总记录数")
    private long total;

    @Schema(description = "总页数")
    private int totalPages;

    @Schema(description = "当前页码（从0开始）")
    private int page;

    @Schema(description = "每页大小")
    private int size;

    @Schema(description = "数据列表")
    private List<T> content;

    public static <T> PageResponse<T> from(Page<T> page) {
        PageResponse<T> response = new PageResponse<>();
        response.setTotal(page.getTotalElements());
        response.setTotalPages(page.getTotalPages());
        response.setPage(page.getNumber());
        response.setSize(page.getSize());
        response.setContent(page.getContent());
        return response;
    }
}